/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.filehandling.oldfileremover;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import softstarter.launching.ItemType;
import softstarter.launching.ItemTypeResolver;
import softstarter.launching.RunnableItem;


public class OldFileRemover {
	
	private ItemTypeResolver itemTypeResolver;
	
	public OldFileRemover() {
		
		itemTypeResolver = new ItemTypeResolver();
	}
	
	public void removeOldFilesFrom(List<RunnableItem> items) {
		
		System.out.println("Remove olds");
		for(Iterator<RunnableItem> iterator = items.iterator(); iterator.hasNext();) {
			
			RunnableItem toBeChecked = iterator.next();
			ItemType type = itemTypeResolver.determineTypeOf(toBeChecked.getFullPath());
			System.out.println(toBeChecked + " " + type);
			boolean remove = false;
			
			switch(type) {
			case FILE:
				System.out.println(toBeChecked + " " + fileExists(toBeChecked.getFullPath()));
				if(! fileExists(toBeChecked.getFullPath())) {
					remove = true;
				}
				
				break;
			case URI:
				remove = false;
				
				break;
			case UNKNOWN:
				remove = true;
				
				break;
			}
			
			if(remove)
				iterator.remove();
			
		}
	}
	
	private boolean fileExists(String fullPath) {
		
		File file = new File(fullPath);
		return file.exists();
	}

	
	
}
